package com.sright.bcard.enterprise.web.exception;

import com.sright.bcard.common.service.share.dto.ResponseResult;
import com.sright.bcard.common.service.share.exception.base.BusinessErrorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class GlobalExceptionHandler {
    public static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 处理自定义的业务异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = BusinessErrorException.class)
    @ResponseBody
    public ResponseResult bizExceptionHandler(HttpServletRequest req, BusinessErrorException e){
        logger.error("企业管理后台发生异常，错误码：{},错误信息：{}",e.getCode(),e.getErrorMsg());
        return ResponseResult.errorResult(e.getCode(),e.getErrorMsg());
    }
}
